package com.fruit.controller.wap.index;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.pojo.recommend.Recommend;
import com.fruit.service.recommend.RecommendManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;
/**
 * 首页controller <br/>
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/")
public class WapIndexController extends BaseController {
	
	@Autowired
	private RecommendManager recommendManager;
	@Autowired
	private UserRightsManager userRightsManager;
	
	@Value("${h5_domain}")
	private String h5Domain;
	@Value("${domain}")
	private String domain;
	@Value("${top_domain}")
	private String topDomain;
	
	/**
	 * 微信支付回调 <br/>
	 * @param request request
	 * @param session session
	 * @return String
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	@RequestMapping
	public String index(HttpServletRequest request, HttpSession session) {
		return "index";
	}
	/**
	 * 登录校验<br/>
	 * 
	 * @param request
	 *            请求
	 * @param session
	 *            session
	 * @return Map<String, Object>
	 */
	@RequestMapping(value = "wap/checklogin", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> checkLogin(HttpServletRequest request, HttpSession session) {
		log.info("【验证登录】---【开始】--");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Integer userId = (Integer) session.getAttribute("userId");
			log.info("【验证登录】---【用户标识】--{}",userId);
			if (userId == null) {
				log.error("【验证登录】---【获取用户标识失败】");
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "error", "获取用户登录异常!"));
				return result;
			}
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "OK", "校验成功!"));
			log.info("【验证登录】---【操作成功】--【用户标识】--{}",userId);
		} catch (Exception e) {
			log.error("【验证登录】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【验证登录】---【结束】--");
		return result;
	}
	
	/**
	 * 首页接口
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param request
	* @param session
	* @return
	 */
	@RequestMapping(value = "wap/wapindex", method = { RequestMethod.GET, RequestMethod.POST })
	public String wapindex(HttpServletRequest request, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Recommend> indexBanner = recommendManager.findRecommendByPosition("indexBanner");
		List<Recommend> goodsRecommend = recommendManager.findRecommendByPosition("goodsRecommend");
		if(goodsRecommend.isEmpty()){
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "没有查询出来数据"));
		}else{
			for(int i = 0;i<goodsRecommend.size();i++){
				Recommend rec = goodsRecommend.get(i);
				//根据字符串生成JSON对象
		        JSONObject resultJson = new JSONObject(rec.getParams());
				rec.setBaseName(resultJson.getString("baseName"));
				rec.setDescribe(resultJson.getString("describe"));
			}
			resultMap.put("goodsRecommend", goodsRecommend);
			resultMap.put("indexBanner", indexBanner);
		}
		List<RightConfig> rightConfig = userRightsManager.getWapClassId();
		resultMap.put("rightConfig", rightConfig);
		return "index";
	}
	
	/**
	 * 链接
	* @author 葛欢欢
	* @version 2017.11.30 16:40 
	* @param request
	* @param session
	* @return
	 */
	@RequestMapping(value = "wap/config", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> config(HttpServletRequest request, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("h5Domain", h5Domain);
		resultMap.put("domain", domain);
		resultMap.put("topDomain", topDomain);
		return resultMap;
	}
	
	
}
